package com.rsc.learningbar.controller;


import com.rsc.learningbar.dao.EbbinghausRepository;
import com.rsc.learningbar.dao.StudentMissionDetailRepository;
import com.rsc.learningbar.dao.StudentPlanRepository;
import com.rsc.learningbar.entity.Ebbinghaus;
import com.rsc.learningbar.entity.StudentMissionDetail;
import com.rsc.learningbar.entity.StudentPlan;
import com.rsc.learningbar.enumer.MemoryType;
import com.rsc.learningbar.result.Result;
import com.rsc.learningbar.vo.StudentMissionDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 学习
 */
@RestController
public class StudentMissionController {

    @Autowired
    StudentMissionDetailRepository studentMissionDetailRepository;

    @Autowired
    StudentPlanRepository studentPlanRepository;

    @Autowired
    MemoryType memoryType;

    @Autowired
    EbbinghausRepository ebbinghausRepository;

    /**
     * 获取题目和答案
     *
     * @param info
     * @return
     */
    @PostMapping("/getStudent")
    public Result<List<StudentMissionDetail>> getStudent(@RequestBody StudentMissionDetailVo info) {
        StudentMissionDetail detail = new StudentMissionDetail();
        detail.setStudentMissionId(info.getStudentMissionId());
        Example<StudentMissionDetail> example = Example.of(detail);
        if (!StringUtils.isEmpty(info.getPlanId())) {
            List<StudentMissionDetail> details = studentMissionDetailRepository.findAll(example, Sort.by(Sort.Direction.ASC, "id"));
            return Result.newSuccess(details);
        }

        List<StudentPlan> plans = studentPlanRepository.findStudentPlan(info.getUserId(), info.getStudentMissionId());
        if (plans != null && plans.size() > 0) {
            return Result.newSuccess(null);
        }
        // 没有任务，插入
        // 查询用户记忆曲线
        Ebbinghaus e = new Ebbinghaus();
        e.setUserId(info.getUserId());
        Example<Ebbinghaus> ex = Example.of(e);
        List<Ebbinghaus> list = ebbinghausRepository.findAll(ex, Sort.by(Sort.Direction.ASC, "type"));
        if (list == null || list.size() <= 0) {
            for (MemoryType.MemoryTrace memoryTrace : memoryType.getTrace()) {
                Ebbinghaus ebbinghaus = new Ebbinghaus();
                ebbinghaus.setUserId(info.getUserId());
                ebbinghaus.setEbbinghaus(memoryTrace.getTime());
                ebbinghaus.setType(memoryTrace.getType());
                ebbinghausRepository.save(ebbinghaus);
            }
        }

        List<StudentMissionDetail> details = studentMissionDetailRepository.findAll(example, Sort.by(Sort.Direction.ASC, "id"));
        return Result.newSuccess(details);
    }

}
